package com.morphiastic.tests.custommapers;

import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.IdentityHashMap;

import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.ElasticStore;
import com.morphiastic.IndexedFieldBridge;
import com.vividsolutions.jts.geom.Point;

public class PointFieldBridge extends IndexedFieldBridge{

	@Override
	public XContentBuilder writeIndexedField(XContentBuilder mappingBuilder,
			String fieldName, Object o, ElasticStore es,
			Integer maxCircularRefDepth, IdentityHashMap<AccessibleObject, Integer> known)
			throws IOException {
		
		if(o==null)return mappingBuilder; //nothing is done if the field is null
		Point point = (Point)o; 
		
		double latitude = point.getY();
		double longitude = point.getX();
		int SRID = point.getSRID();
		
		mappingBuilder.startObject(fieldName)
			.field("latitude", latitude)
			.field("longitude",longitude)
//			.field("SRID", SRID)
		.endObject();
		
		 return mappingBuilder;
	}

	@Override
	public void addIndexFieldSettings(XContentBuilder mappingBuilder)
			throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addFieldSettingsMap(HashMap<String, Object> props) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(ElasticStore es, HashMap<?, ?> params) {
	}

}
